package com.lxp.eduService.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxp.eduService.domain.EduComment;
import com.lxp.eduService.domain.EduCourse;
import com.lxp.eduService.domain.vo.chapter.ChapterVo;
import com.lxp.eduService.domain.vo.front.CommentVo;
import com.lxp.eduService.domain.vo.front.CourseFrontInfoVo;
import com.lxp.eduService.domain.vo.queryVo.CourseFrontVo;
import com.lxp.eduService.service.EduChapterService;
import com.lxp.eduService.service.EduCommentService;
import com.lxp.eduService.service.EduCourseService;
import com.lxp.utils.R;
import com.lxp.utils.vo.orderVo.CourseFrontOrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
//前端控制器 用来获取课程信息
@RequestMapping("/eduCourse/front")

public class CourseFrontController {

    @Autowired
    private EduCourseService service;
    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduCommentService commentService;
    //条件分页查询课程
    @PostMapping("/getCourseDataPage/{current}/{limit}")
    public R getCourseDataPage(@PathVariable long current,
                               @PathVariable long limit,
                               @RequestBody(required = false) CourseFrontVo courseFrontVo){

        Page<EduCourse> page =new Page<>(current,limit);
        Map<String,Object> map =service.getCourseListFront(page,courseFrontVo);
        return R.ok().data(map);
    }

    //获取课程详情页
    @GetMapping("/getCourseInfo/{courseId}")
    public  R getCourseInfo(@PathVariable String courseId){
        //查询章节信息
        List<ChapterVo> chapterVideo = chapterService.getChapterVideo(courseId);
        //查询课程详情
        CourseFrontInfoVo courseInfoList=service.getCourseFrontInfo(courseId);
        return  R.ok().data("chapterInfo",chapterVideo).data("courseInfoList",courseInfoList);
    }

    //client获取课程详情页
    @GetMapping("/getClientCourseInfo/{courseId}")
    public CourseFrontOrderVo getClientCourseInfo(@PathVariable String courseId){
        //查询章节信息
        CourseFrontOrderVo courseFrontOrderVo=new CourseFrontOrderVo();
        //查询课程详情
        CourseFrontInfoVo courseInfoList=service.getCourseFrontInfo(courseId);
        BeanUtils.copyProperties(courseInfoList,courseFrontOrderVo);
        return courseFrontOrderVo;
    }

    //添加课程评论
    @PostMapping("/addComment")
    public R addComment(@RequestBody EduComment comment){
        commentService.save(comment);
        return R.ok();
    }

    //分页获取评论列表
    @GetMapping("/getComment/{current}/{limit}/{courseId}")
    public R getComment(@PathVariable long current,@PathVariable long limit,@PathVariable String courseId){
        Page<EduComment> page =new Page<>(current,limit);
        commentService.page(page,new QueryWrapper<EduComment>().orderByDesc("gmt_create")
        .eq("course_id",courseId)
        .eq("to_comment_id",""));

        List<CommentVo> list=new ArrayList<>();
        for (EduComment record : page.getRecords()) {
            CommentVo commentVo =new CommentVo();
            BeanUtils.copyProperties(record,commentVo);
            commentVo.setChildrenComment(commentService.count(new QueryWrapper<EduComment>()
                    .eq("to_comment_id",record.getId())));
            list.add(commentVo);
        }
        return R.ok().data("total",page.getTotal()).data("items",list);
    }

    //搜索框搜索分页显示
    @GetMapping("/selectByName/{current}/{limit}/{courseName}")
    public R selectByName(@PathVariable String courseName,
                          @PathVariable long current,
                          @PathVariable long limit){
        Page<EduCourse> page = service.selectByName(courseName,current,limit);
        return R.ok().data("items",page.getRecords()).data("total",page.getTotal());
    }

    //点击查看回复详情
    @GetMapping("/getCommentMore/{commentId}")
    public R getCommentMore(@PathVariable String commentId){
        List<EduComment> list=service.getCommentMore(commentId);
        return R.ok().data("list",list);
    }


}
